package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Option;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.OptionManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;

@Controller
@Scope(value = "prototype")
@RequestMapping(value = "/admin/option")
public class OptionController extends BaseController {

    private static final long serialVersionUID = -8730779611525448972L;

    private OptionManager     optionManager;
    private Option            option;

    /**
     * 添加选项
     * 
     * @param requ
     * @param ec
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, Integer questionId, OptionController ec) {
        ModelAndView mav = new ModelAndView("");
        option = ec.getOption();
        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                option.setCreatetime(getSqlDate());
                option.setCreateuser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                optionManager.add(option);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        if(null != questionId){
            mav.addObject("questionId", questionId);
        }
        mav.setViewName("admin/option/add");
        mav.addObject("message", getMessage());
        return mav;
    }

    /**
     * 修改选项
     * 
     * @param requ
     * @param ec
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, OptionController ec) {
        ModelAndView mav = new ModelAndView("");
        option = ec.getOption();
        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                option.setUpdatetime(getSqlDate());
                option.setUpdateuser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                optionManager.modify(option);
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }else if (ec.getOption() != null && ec.getOption().getId() != null) {
            option = optionManager.get(ec.getOption().getId());
        } else {
            option = new Option();
        }
        mav.addObject("option", option);
        mav.setViewName("admin/option/edit");
        return mav;
    }

    /**
     * 删除选项，未实现，考虑使用use_yn实现删除效果
     * 
     * @param requ
     * @return
     */
    @RequestMapping(value = "/delete")
    public ModelAndView delete(HttpServletRequest requ) {
        ModelAndView mav = new ModelAndView("");
        // TODO 选项下有选项，或者已经有答卷不能删除选项
        return mav;
    }

    /**
     * 分页查询选项
     * 
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/page")
    public ModelAndView page(OptionController ec, Integer pageNo, Integer pageSize) {
        ModelAndView mav = new ModelAndView("");
        option = ec.getOption();
        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        IPage<Option> page = optionManager.pageSearch(option, pageNo, pageSize);
        mav.addObject("page", page);
        return mav;
    }

    public OptionManager getOptionManager() {
        return optionManager;
    }

    public void setOptionManager(OptionManager optionManager) {
        this.optionManager = optionManager;
    }

    public Option getOption() {
        return option;
    }

    public void setOption(Option option) {
        this.option = option;
    }
}
